/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.billy.ohos.swipe.ext.refresh;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import java.util.Arrays;

final class ScaleHelper {
    private float[] mScales;

    ScaleHelper(float... scales) {
        this.updateScales(scales);
    }

    void updateScales(float... scales) {
        if (scales.length == 0) {
            scales = new float[]{1.0F, 0.0F, 1.0F, 1.0F};
        }

        float[] var2 = scales;
        int var3 = scales.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            float tmp = var2[var4];
            if (tmp < 0.0F) {
                throw new IllegalArgumentException("Array value can not be negative!");
            }
        }

        if (!Arrays.equals(this.mScales, scales)) {
            if (scales.length < 2 || scales.length % 2 != 0) {
                throw new IllegalArgumentException("Array length no match!");
            }

            this.mScales = scales;
            this.appendIfNeed();
            this.checkIsArrayLegal();
        }

    }

    float getScale(float fraction) {
        float minScale = 1.0F;
        float maxScale = 1.0F;
        float minFraction = 0.0F;
        float maxFraction = 1.0F;

        float scalePosition;
        int i;
        for(i = 1; i < this.mScales.length; i += 2) {
            scalePosition = this.mScales[i];
            if (scalePosition > fraction) {
                break;
            }

            minScale = this.mScales[i - 1];
            minFraction = this.mScales[i];
        }

        for(i = this.mScales.length - 1; i >= 1; i -= 2) {
            scalePosition = this.mScales[i];
            if (scalePosition < fraction) {
                break;
            }

            maxScale = this.mScales[i - 1];
            maxFraction = this.mScales[i];
        }

        fraction = this.solveTwoPointForm(minFraction, maxFraction, fraction);
        float distance = maxScale - minScale;
        float scale = distance * fraction;
        float result = minScale + scale;
        return this.isFinite(result) ? result : minScale;
    }

    private float solveTwoPointForm(float startX, float endX, float currentX) {
        return (currentX - startX) / (endX - startX);
    }

    private boolean isFinite(float value) {
        return !Float.isNaN(value) && !Float.isInfinite(value);
    }

    private void appendIfNeed() {
        if (this.mScales[1] != 0.0F) {
            this.mScales = this.insertElement(true, this.mScales, 1.0F, 0.0F);
        }

        if (this.mScales[this.mScales.length - 1] != 1.0F) {
            this.mScales = this.insertElement(false, this.mScales, 1.0F, 1.0F);
        }

    }

    private void checkIsArrayLegal() {
        float min = this.mScales[1];

        for(int i = 1; i < this.mScales.length; i += 2) {
            float temp = this.mScales[i];
            if (min > temp) {
                throw new IllegalArgumentException("Incorrect array value! position must be from small to large");
            }

            min = temp;
        }

    }

    private float[] insertElement(boolean isAddFromHead, float[] target, float... elements) {
        float[] result = new float[target.length + elements.length];
        if (isAddFromHead) {
            System.arraycopy(elements, 0, result, 0, elements.length);
            System.arraycopy(target, 0, result, elements.length, target.length);
        } else {
            System.arraycopy(target, 0, result, 0, target.length);
            System.arraycopy(elements, 0, result, target.length, elements.length);
        }

        return result;
    }
}

